# (MSYS2) Maintainer: Ray Donnelly <mingw.android@gmail.com>
# (ArchLinux) Maintainer: Gustavo Alvarez <sl1pkn07@gmail.com>
# (ArchLinux) Contributor: Bruno Pagani (a.k.a. ArchangeGabriel) <bruno.n.pagani at gmail dot com>

# For now as I've not got shared KF5 ready.
_variant=-static
#_variant=-shared

_basename=krita
_realname=${_basename}-git
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.0.89.r39172.46b0002
pkgrel=1
pkgdesc="A free digital painting application. Digital Painting, Creative Freedom!. (GIT Version) (mingw-w64)"
arch=('any')
url="https://www.krita.org/"
license=('GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-gsl"
         "${MINGW_PACKAGE_PREFIX}-LibRaw"
         "${MINGW_PACKAGE_PREFIX}-opencolorio"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-exiv2"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-poppler"
#         "${MINGW_PACKAGE_PREFIX}-kio"
         "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
         )
makedepends=("${MINGW_PACKAGE_PREFIX}-extra-cmake-modules"
             "git"
             "${MINGW_PACKAGE_PREFIX}-boost"
#             "${MINGW_PACKAGE_PREFIX}-vc"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             )
provides=("${MINGW_PACKAGE_PREFIX}-krita")
conflicts=("${MINGW_PACKAGE_PREFIX}-krita")
install=krita-git.install
source=('git+https://anongit.kde.org/krita.git')
sha256sums=('SKIP')

pkgver() {
  cd "${srcdir}"/${_basename}
  _ver="$(cat CMakeLists.txt | grep -m3 -e CALLIGRA_STABLE_VERSION_MAJOR -e CALLIGRA_STABLE_VERSION_MINOR -e CALLIGRA_VERSION_RELEASE | cut -d ')' -f1 | grep -o "[[:digit:]]*" | paste -sd'.')"
  echo "${_ver}.r$(git rev-list --count HEAD).$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}"/${_basename}
}

build() {
  local -a extra_config
  mkdir -p build-${CARCH}${_variant}
  cd build-${CARCH}${_variant}
  if [ "${_variant}" = "-static" ]; then
    extra_config+=( -DBUILD_SHARED_LIBS=NO )
    QT5_PREFIX=${MINGW_PREFIX}/qt5-static
    export PATH=${QT5_PREFIX}/bin:"$PATH"
  else
    QT5_PREFIX=${MINGW_PREFIX}
  fi
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  cmake ../${_basename} \
    -DCMAKE_INSTALL_PREFIX=${QT5_PREFIX} \
    -DLIB_INSTALL_DIR=lib \
    -DECM_MKSPECS_INSTALL_DIR=${QT5_PREFIX} \
    -DQt5_DIR=${QT5_PREFIX}/lib/cmake/Qt5 \
    -DBUILD_TESTING=OFF \
    -DECM_DIR=${MINGW_PREFIX}/share/ECM \
    "${extra_config[@]}" \
    -G'MSYS Makefiles'
  make
}

package() {
  cd build-${CARCH}${_variant}
  make DESTDIR="${pkgdir}" install
}
